local sk__zhonghun = fk.CreateSkill {

  name = "sk__zhonghun",

  tags = { Skill.Limited, },

}



sk__zhonghun:addEffect("active", {
  name = "sk__zhonghun",
  anim_type = "big",
  card_num = 0,
  mute = true,
  target_num = 1,
  prompt = "#sk__zhonghun-choose",
  can_use = function(self, player)
    return player:usedSkillTimes(sk__zhonghun.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    player:broadcastSkillInvoke("sk__zhonghun")
    room:notifySkillInvoked(player, "sk__zhonghun", "big")
    room:changeMaxHp(player, -1)
    if target.dead then return end
    room:changeMaxHp(target, 1)
    if target.dead then return end
    if target:isWounded() then
      room:recover({
        who = target,
        num = 1,
        recoverBy = player,
        skillName = sk__zhonghun.name,
      })
    end
    if player.dead then return end
    room:setPlayerMark(player, "@sk__zhonghun", target.general)
    room:setPlayerMark(player, sk__zhonghun.name, target.id)
  end,
})

sk__zhonghun:addEffect(fk.GameStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(sk__zhonghun.name) and #player.room.alive_players > 1 and
        player:usedSkillTimes("sk__zhonghun", Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), Util.IdMapper), 1,
      1,
      "#sk__zhonghun-choose", "sk__zhonghun", true, false)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__zhonghun")
    room:notifySkillInvoked(player, "sk__zhonghun")
    local skillOnUse = function(room, effect)
      local player = effect.from
      local target = effect.tos[1]
      room:changeMaxHp(player, -1)
      if target.dead then return end
      room:changeMaxHp(target, 1)
      if target.dead then return end
      if target:isWounded() then
        room:recover({
          who = target,
          num = 1,
          recoverBy = player,
          skillName = sk__zhonghun.name,
        })
      end
      if player.dead then return end
      room:setPlayerMark(player, "@sk__zhonghun", target.general)
      room:setPlayerMark(player, sk__zhonghun.name, target.id)
    end
    skillOnUse(room, {
      from = player,
      tos = { room:getPlayerById(event:getCostData(self)) },
    })
  end,
})

sk__zhonghun:addEffect(fk.DamageInflicted, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("sk__zhonghun") ~= 0 then
      return player:getMark("sk__zhonghun") == target.id and (not data.from or data.from ~= player)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__zhonghun")
    room:notifySkillInvoked(player, "sk__zhonghun", "defensive")
    data.to = player
  end,
})
sk__zhonghun:addEffect(fk.Death, {
  name = "#sk__zhonghun_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("sk__zhonghun") ~= 0 then
      return target == player and not player.room:getPlayerById(player:getMark("sk__zhonghun")).dead
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__zhonghun")
    room:notifySkillInvoked(player, "sk__zhonghun", "defensive")
    local to = room:getPlayerById(player:getMark("sk__zhonghun"))
    room:doIndicate(player.id, { to.id })
    local skills = {}
    for _, s in ipairs(player.player_skills) do
      if s:isPlayerSkill(player) and s.visible and not to:hasSkill(s, true) then
        table.insert(skills, s.name)
      end
    end
    room:handleAddLoseSkills(to, skills, nil, true, false)
  end,
})
sk__zhonghun:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    return player:usedSkillTimes("sk__zhonghun", Player.HistoryGame) > 0 and player:getMark("sk__zhonghun") == target.id
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@sk__zhonghun", 0)
    player.room:setPlayerMark(player, "sk__zhonghun", 0)
  end,
})

return sk__zhonghun
